package com.zhao.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

//获取SpringBoot创建好的工厂
@Configuration
@Slf4j
public class ApplicationContextUtils implements ApplicationContextAware {

    //保留下来的工厂
    private static ApplicationContext applicationContext;

    //将创建好的工厂以参数形式传递给这个类
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //提供在工厂中获取对象的方法
    //RedisTemplate redisTemplate
    public static Object getBean(String beanName) {
        if (beanName == null && beanName.equals(" ")) {
            return null;
        }
        return applicationContext.getBean(beanName);
    }
}
